package com.ford.trade.processor;

import com.ford.trade.entity.PeiLv;
import com.ford.trade.service.Game500Service;
import com.ford.trade.service.PeilvService;
import lombok.Data;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.Date;

/**
 * @author: ford
 * @date: 2022/4/23 13:41
 */
@Data
@Component
public class PeilvProcessor implements PageProcessor {

    private PeilvService peilvService;
    private Game500Service gameService;

    private String type;
    private String date;
    private String cookie;

    @Override
    public void process(Page page) {

        PeiLv peiLv = new PeiLv();
        Document document = page.getHtml().getDocument();
        String url = page.getUrl().get();
        String type = "";
        Long indexId = Long.valueOf(url.substring(url.lastIndexOf("-") + 1, url.lastIndexOf(".")));
        int length = url.length();
        if (url.contains("ctype")) {
            if ("1".equals(url.substring(length - 1))) {
                type = "全部公司";
            } else if ("2".equals(url.substring(length - 1))) {
                type = "主流";
            } else if ("3".equals(url.substring(length - 1))) {
                type = "交易所";
            } else if ("4".equals(url.substring(length - 1))) {
                type = "非交易所";
            }
        } else if (url.contains("order")) {
            if ("0".equals(url.substring(length - 1))) {
                type = "威立I";
            } else if ("1".equals(url.substring(length - 1))) {
                type = "威立BI";
            } else if ("2".equals(url.substring(length - 1))) {
                type = "威立澳BI";
            } else if ("4".equals(url.substring(length - 1))) {
                type = "竞彩6";
            }
        }
        peiLv.setIndexId(indexId);
        peiLv.setType(type);
        //初始胜
        peiLv.setSheng(Double.valueOf(document.getElementById("avwinc2").text()));
        //初始平
        peiLv.setPing(Double.valueOf(document.getElementById("avdrawc2").text()));
        //初始负
        peiLv.setFu(Double.valueOf(document.getElementById("avlostc2").text()));
        //变化胜
        peiLv.setShengChange(Double.valueOf(document.getElementById("avwinj2").text()));
        //变化平
        peiLv.setPingChange(Double.valueOf(document.getElementById("avdrawj2").text()));
        //变化负
        peiLv.setFuChange(Double.valueOf(document.getElementById("avlostj2").text()));
        //返还率
        peiLv.setReturnRate(document.getElementById("avpaylc2").text());
        //及时返还率
        peiLv.setReturnRateChange(document.getElementById("avpaylj2").text());
        //初始胜凯利
        peiLv.setShengKelly(Double.valueOf(document.getElementById("avklwc2").text()));
        //初始平凯利
        peiLv.setPingKelly(Double.valueOf(document.getElementById("avkldc2").text()));
        //初始负凯利
        peiLv.setFuKelly(Double.valueOf(document.getElementById("avkllc2").text()));
        //变化胜凯利
        peiLv.setShengKellyChange(Double.valueOf(document.getElementById("avklwj2").text()));
        //变化平凯利
        peiLv.setPingKellyChange(Double.valueOf(document.getElementById("avkldj2").text()));
        //变化负凯利
        peiLv.setFuKellyChange(Double.valueOf(document.getElementById("avkllj2").text()));


        peiLv.setDatetime(new Date());
        peilvService.save(peiLv);

    }

    private Site site = Site.me()
            .setCharset("gb2312")
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
            .addCookie("cookie", cookie)
            .setTimeOut(1000000)
            .setRetrySleepTime(10000)
            .setRetryTimes(3);

    @Override
    public Site getSite() {
        return site;
    }

}
